/*
 * ========THE SOLMIX PROJECT=====================================
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.gnu.org/licenses/ 
 * or see the FSF site: http://www.fsf.org. 
 */
package org.solmix.fmk;

import java.util.Hashtable;
import java.util.Locale;
import java.util.ResourceBundle;

import org.solmix.commons.collections.DataTypeMap;
import org.solmix.fmk.i18n.ResourceBundleManagerImpl;
import org.solmix.fmk.servlet.RequestContext;
import org.solmix.fmk.util.MessageTools;
import org.solmix.fmk.util.OSGIHelper;
import org.solmix.services.i18n.ResourceBundleManager;


/**
 * 
 * @author solomon
 * @version $Id$  2011-3-16
 */

@SuppressWarnings("unchecked")
public final class VirtualContext extends Hashtable
{

   /**
    * 
    */
   private static final long serialVersionUID = -4923063229643617796L;

   private MessageTools msgTool;

   private DataTypeMap globalConfig;

   private String language;

   
   public DataTypeMap getGlobalConfig()
   {
      if (globalConfig == null)
      {
         if (this.name.equals(VirtualManager.DEFAULT))
            globalConfig = OSGIHelper.getCM().getSubtree("");
         else
            globalConfig = OSGIHelper.getCM().getSubtree(name);
      }
      return globalConfig;
   }

   public void setGlobalConfig(DataTypeMap globalConfig)
   {
      this.globalConfig = globalConfig;
   }

   public MessageTools getMsgTool()
   {
      if (msgTool == null)
      {
         ResourceBundleManager rbm = new ResourceBundleManagerImpl();

         ResourceBundle bundle = rbm.getResourceBundle(new Locale(getLanguage()));
         msgTool = new MessageTools(bundle, this);
      }
      return msgTool;
   }

   public void setMsgTool(MessageTools msgTool)
   {
      this.msgTool = msgTool;
   }


   public String getLanguage()
   {
      if (language == null)
         language = getGlobalConfig().getString("language", Locale.ENGLISH.getLanguage());
      return language;
   }

   public void setLanguage(String language)
   {
      this.language = language;
   }

   private final String name;

   VirtualContext( String name, RequestContext context )
   {
      this.name = name;
   }

}
